﻿using System;
using System.ComponentModel;
using System.Web.UI;
using System.Collections;
using System.Collections.Generic;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.Core;

namespace Yuanlin.Business
{
    public class ProductList : YuanlinUserControl
    {
        protected Repeater list;
        protected Pager pager;
        protected CurrentPage currentPage;
        private string queryText = UrlManager.Instance.GetParameterValue<string>(YuanlinParameterName.QueryText);
        private string character = UrlManager.Instance.GetParameterValue<string>(YuanlinParameterName.Character);
        private int miaoMuCategoryId = UrlManager.Instance.GetParameterValue<int>(YuanlinParameterName.MiaoMuCategoryId);
        public int categoryType = UrlManager.Instance.GetParameterValue<int>(YuanlinParameterName.CategoryType);

        protected override void OnLoad(EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                BindData();
            }
        }

        public void BindData()
        {
            int productTotalCount = 0;
            EntityList dataSource = null;

            if (!string.IsNullOrEmpty(character))
            {
                EntityList currentPageProducts = new EntityList();
                EntityList resultProducts = new EntityList();

                ProductListRequest request = new ProductListRequest();
                request.Title = queryText;
                EntityList products = BusinessManager.GetProductList(RequestHelper.BuildGetProductListRequest(request));
                foreach (Product product in products)
                {
                    string temp = ChineseConvertor.Convert(product.Title.Value);
                    if (!string.IsNullOrEmpty(temp) && temp[0].ToString().ToLower() == character.ToLower())
                    {
                        resultProducts.Add(product);
                    }
                }
                for (int i = pager.PageIndex * pager.PageSize; i < resultProducts.Count && i < (pager.PageIndex + 1) * pager.PageSize; i++)
                {
                    currentPageProducts.Add(resultProducts[i]);
                }
                dataSource = currentPageProducts;
                productTotalCount = resultProducts.Count;
            }
            else
            {
                ProductListRequest request = new ProductListRequest();
                request.MiaomuCategoryId = miaoMuCategoryId;
                request.Title = queryText;
                request.PageIndex = pager.PageIndex;
                request.PageSize = pager.PageSize;
                EntityList products = BusinessManager.GetProductList(RequestHelper.BuildGetProductListRequest(request));
                dataSource = products;
                productTotalCount = products.TotalCount;
            }


            list.DataSource = dataSource;
            list.DataBind();

            if (pager != null && currentPage != null)
            {
                pager.TotalPages = pager.CalculateTotalPages(productTotalCount);
                currentPage.TotalRecords = productTotalCount;
                currentPage.TotalPages = pager.TotalPages;
                currentPage.PageIndex = pager.PageIndex;
            }
        }

    }
}
